<?php
if ( IPB_THIS_SCRIPT != 'admin' )
{
	print "<h1>Incorrect access</h1>You cannot access this file directly. If you have recently upgraded, make sure you upgraded 'init.php'.";
	exit();
}

class ad_plugin
{
	var $ipsclass;
	
	function auto_run()
	{
		$code_redirect = array(
								'list' => 'list_plugins',
								'install' => 'install_plugin',
								'do_install' => 'do_install_plugin',
								'run' => 'run_plugin',
								'edit' => 'edit_plugin',
								'do_edit' => 'save_plugin_settings',
								'permissions' => 'edit_permissions',
								'do_permission' => 'save_permissions',
								'uninstall' => 'uninstall_plugin',
								'move' => 'change_position',
								'force' => 'force_uninstall',
							);
		if(!$code_redirect[$this->ipsclass->input['do']])
		{
			$this->ipsclass->input['do'] = 'list';
		}
		$run = $code_redirect[$this->ipsclass->input['do']];
		$this->$run();		
	}
	
	function list_plugins()
	{
		$this->ipsclass->admin->page_title  = "Welcome to managing plugins for ibMarket";
		$this->ipsclass->admin->page_detail = "Welcome to managing the plugins for ibMarket.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=plugin', 'Plugin Control' );
		
		$this->ipsclass->adskin->td_header[] = array( "Link ID"			, "10%" );
		$this->ipsclass->adskin->td_header[] = array( "Name"			, "80%" );
		$this->ipsclass->adskin->td_header[] = array( "Position"		, "5%" );
		$this->ipsclass->adskin->td_header[] = array( "Options"			, "5%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Plugin List");
		
		if(is_array($this->ipsclass->cache['market_plugin']) and count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $link => $plugin)
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																					$link,
																					$plugin['name'],
																					"<center>".($plugin['position'] == 1 ? "" : "<a href='{$this->ipsclass->base_url}&amp;{$this->ipsclass->form_code}&amp;code=plugin&amp;do=move&amp;move=up&amp;link={$link}' title='Move up in position'><img src='{$this->ipsclass->skin_acp_url}/images/arrow_up.png' width='12' height='12' border='0' style='vertical-align:middle' /></a>")."   ".($plugin['position'] == count($this->ipsclass->cache['market_plugin']) ? "" : "<a href='{$this->ipsclass->base_url}&amp;{$this->ipsclass->form_code}&amp;code=plugin&amp;do=move&amp;move=down&amp;link={$link}' title='Move down in position'><img src='{$this->ipsclass->skin_acp_url}/images/arrow_down.png' width='12' height='12' border='0' style='vertical-align:middle' /></a>")."</center>",
																					"<img id=\"menu_{$link}\" src='{$this->ipsclass->vars['board_url']}/skin_acp/IPB2_Standard/images/filebrowser_action.gif' border='0' alt='Options' class='ipd' style='float:right'/>",
																			)	);
				
				$this->ipsclass->html .= "<script type=\"text/javascript\">
	menu_build_menu( \"menu_{$link}\",
  new Array(
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=plugin&do=edit&link=".$link."'>Edit Settings</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=plugin&do=permissions&link=".$link."'>Edit Permissions</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=plugin&do=run&link=".$link."'>Run Plugin</a>\",
			img_item   + \" <a href='{$this->ipsclass->base_url}&{$this->ipsclass->form_code_js}&code=plugin&do=uninstall&link=".$link."'>Uninstall Plugin</a>\"
      )     );
 </script>";
			}
		}
		else
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_basic("No plugins installed",'center');
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$files = $this->ipsclass->market_class->get_file_list(ROOT_PATH."sources/market/plugins",0);
		$can_install = array();
		$leftovers = array();
		foreach($files as $file)
		{
			if(strstr($file, 'template_plugin') !== false || substr($file,-4) != '.php')
			{
				continue;
			}
			
			$count = $this->ipsclass->market_class->count_plugin_filename($file);
			$plugin = $this->ipsclass->market_class->load_plugin(array('link'=>$file,'filename'=>$file));
			if($count == 0)
			{
				$leftovers[] = array($file,$plugin->name);
			}
			if($plugin->multiple || $count == 0)
			{
				$can_install[] = array($file,$plugin->name);
			}
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'plugin'      ),
																			 4 => array( 'do'        , 'install'     ),
																	)		);
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Install a Plugin");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Which plugin?</b>",
																			$this->ipsclass->adskin->form_dropdown('plugin',$can_install),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Install Plugin");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'plugin'      ),
																			 4 => array( 'do'        , 'force'       ),
																	)		);
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Force uninstall a Plugin");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Which plugin?</b>",
																			$this->ipsclass->adskin->form_dropdown('plugin',$leftovers),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Uninstall Plugin");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->admin->output();
	}
	
	function install_plugin()
	{
		if(!file_exists(ROOT_PATH."sources/market/plugins/".$this->ipsclass->input['plugin']) || strstr($this->ipsclass->input['plugin'], 'template_plugin') !== false)
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$plugin = $this->ipsclass->market_class->load_plugin(array('filename'=>$this->ipsclass->input['plugin']));
		$class = $this->ipsclass->market_class->get_class_name($this->ipsclass->input['plugin']);
		
		if(!$plugin->multiple)
		{
			if($this->ipsclass->market_class->count_plugin_filename($this->ipsclass->input['plugin']) > 0)
			{
				$this->ipsclass->admin->error("The plugin cannot be installed multiple times");
			}
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to installing a plugin into ibMarket";
		$this->ipsclass->admin->page_detail = "Welcome to installing a plugin into ibMarket.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=plugin', 'Plugin Control'    );
		$this->ipsclass->admin->nav[] 		= array( ''                                           , 'Installing Plugin' );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'plugin'      ),
																			 4 => array( 'do'        , 'do_install'  ),
																			 5 => array( 'plugin'     , $this->ipsclass->input['plugin']),
																	),'theAdminForm',"onsubmit='return ValidateForm()'");
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Installing the ".$plugin->name);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Link ID?</b><br />This is for url part. No two link id can be the same.<br />Example: ".$this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?autocom=market&amp;plugin={link id}",
																			$this->ipsclass->adskin->form_input('link',$class),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>What is the name for this specific plugin?</b>",
																			$this->ipsclass->adskin->form_input('name',$plugin->name),
																	)	);
		
		$this->ipsclass->html .= $plugin->acp_settings();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->build_permissions(array(),array());
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form_standalone("Install Plugin");
		
		$this->ipsclass->admin->output();
	}
	
	function build_permissions($view_perms = array(-1), $use_perms = array(-1))
	{
		$this->ipsclass->adskin->td_header[] = array( "Group Name"  , "34%" );
		$this->ipsclass->adskin->td_header[] = array( "View Plugin" , "33%" );
		$this->ipsclass->adskin->td_header[] = array( "Use Plugin"  , "33%" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Plugin Permission");
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<div align='right'><i>GLOBAL PERMISSION</i></div>",
																			"<div align='center' style='border:1px solid #777777;background-color: #DFE6EF'>".$this->ipsclass->adskin->form_checkbox('VIEW[-1]',in_array(-1,$view_perms),-1,array("id='VIEW[-1]'","onclick='check_all(\"VIEW\")'"," style='border: none'"))."</div>",
																			"<div align='center' style='border:1px solid #777777;background-color: #f5cdcd'>".$this->ipsclass->adskin->form_checkbox('USE[-1]',in_array(-1,$use_perms),-1,array("id='USE[-1]'","onclick='check_all(\"USE\")'"," style='border: none'"))."</div>",
																	)	);
		
		foreach($this->ipsclass->cache['group_cache'] as $g_id => $group)
		{
			$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																				"<div align='right'>".$group['prefix'].$group['g_title'].$group['suffix']."&nbsp;&nbsp;<input type='button' id='button' value='+' onclick='check_row(".$g_id.",true)' />&nbsp;<input type='button' id='button' value='-' onclick='check_row(".$g_id.",false)' /></div>",
																				"<div align='center' style='border:1px solid #777777;background-color: #DFE6EF'>".$this->ipsclass->adskin->form_checkbox('VIEW['.$g_id.']',in_array(-1,$view_perms) || in_array($g_id,$view_perms),$g_id,array("id='VIEW[".$g_id."]'","onclick='full_check(\"VIEW\")'"," style='border: none'"))."</div>",
																				"<div align='center' style='border:1px solid #777777;background-color: #f5cdcd'>".$this->ipsclass->adskin->form_checkbox('USE['.$g_id.']',in_array(-1,$use_perms) || in_array($g_id,$use_perms),$g_id,array("id='USE[".$g_id."]'","onclick='full_check(\"USE\")'"," style='border: none'"))."</div>",
																		)	);
		}
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"&nbsp;",
																			"<div align='center'><input type='button' id='button' value='+' onclick='check_col(\"VIEW\",true)' />&nbsp;<input type='button' id='button' value='-' onclick='check_col(\"VIEW\",false)' /></div>",
																			"<div align='center'><input type='button' id='button' value='+' onclick='check_col(\"USE\",true)' />&nbsp;<input type='button' id='button' value='-' onclick='check_col(\"USE\",false)' /></div>",
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
	}
	
	function do_install_plugin()
	{
		if(!file_exists(ROOT_PATH."sources/market/plugins/".$this->ipsclass->input['plugin']) || strstr($this->ipsclass->input['plugin'], 'template_plugin') !== false)
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$plugin = $this->ipsclass->market_class->load_plugin(array('filename'=>$this->ipsclass->input['plugin']));
		
		if(isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
		{
			$this->ipsclass->admin->error("The plugin link id has already been used");
		}
		
		if(!$plugin->multiple)
		{
			if($this->ipsclass->market_class->count_plugin_filename($this->ipsclass->input['plugin']) > 0)
			{
				$this->ipsclass->admin->error("The plugin cannot be installed multiple times");
			}
		}
		
		$this->check_link($this->ipsclass->input['link']);
		
		$input = array();
		$input['link'] = $this->ipsclass->input['link'];
		$input['name'] = $this->ipsclass->input['name'];
		$input['filename'] = $this->ipsclass->input['plugin'];
		
		if(is_array($this->ipsclass->input['VIEW']))
		{
			if(in_array(-1,$this->ipsclass->input['VIEW']))
			{
				$this->ipsclass->input['VIEW'] = array(-1);
			}
			$input['view_permission'] = $this->ipsclass->input['VIEW'];
		}
		else
		{
			$input['view_permission'] = array();
		}
		
		if(is_array($this->ipsclass->input['USE']))
		{
			if(in_array(-1,$this->ipsclass->input['USE']))
			{
				$this->ipsclass->input['USE'] = array(-1);
			}
			$input['use_permission'] = $this->ipsclass->input['USE'];
		}
		else
		{
			$input['use_permission'] = array();
		}
		
		$plugin->acp_save_settings($input);
		$plugin->acp_install($input);
		
		$input['view_permission'] = implode(",",$input['view_permission']);
		$input['use_permission'] = implode(",",$input['use_permission']);
		$input['position'] = count($this->ipsclass->cache['market_plugin'])+1;
		
		$this->ipsclass->DB->do_insert('market_plugins',$input);
		
		$this->ipsclass->market_class->cache_plugins();
		
		$this->ipsclass->admin->save_log("ibMarket: Installed plugin ".$this->ipsclass->input['name']." (plugin: ".$plugin->name.")");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=plugin","Plugin Installed");
	}
	
	function run_plugin()
	{
		if(!isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
		{
			$this->ipsclass->admin->error("ibMarket: Illegal plugin");
		}
		
		$this->ipsclass->old_form_code    = $this->ipsclass->form_code;
		$this->ipsclass->old_form_code_js = $this->ipsclass->form_code_js;
		$this->ipsclass->form_code    = $this->ipsclass->form_code .    '&amp;code=plugin&amp;do=run&amp;link=' . $this->ipsclass->input['link'];
		$this->ipsclass->form_code_js = $this->ipsclass->form_code_js . '&code=plugin&do=run&link='             . $this->ipsclass->input['link'];
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->old_form_code.'&amp;code=plugin', 'Plugin Control' );
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code                       , 'Run Plugin'     );
		
		$plugin = $this->ipsclass->market_class->load_plugin($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]);
		$plugin->acp();
		
		// Still here
		// Error out.
		$this->ipsclass->admin->error("This plugin cannot be runned");
	}
	
	function edit_plugin()
	{
		if(!isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$plugin = $this->ipsclass->market_class->load_plugin($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]);
		
		$this->ipsclass->admin->page_title  = "Welcome to editing a plugin into ibMarket";
		$this->ipsclass->admin->page_detail = "Welcome to editing a plugin into ibMarket.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=plugin', 'Plugin Control'          );
		$this->ipsclass->admin->nav[] 		= array( ''                                           , 'Editing Plugin Settings' );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'  ),
																			 2 => array( 'act'       , 'market'      ),
																			 3 => array( 'code'      , 'plugin'      ),
																			 4 => array( 'do'        , 'do_edit'     ),
																			 5 => array( 'plugin'     , $this->ipsclass->input['link']),
																	),'theAdminForm',"onsubmit='return ValidateForm()'");
		
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		$this->ipsclass->adskin->td_header[] = array( "" , "" );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Editing the ".$this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]['name']);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>Link ID?</b><br />This is for url part. No two link id can be the same.<br />Example: ".$this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?autocom=market&amp;plugin={link id}",
																			$this->ipsclass->adskin->form_input('link',$this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]['link']),
																	)	);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																			"<b>What is the name for this specific plugin?</b>",
																			$this->ipsclass->adskin->form_input('name',$this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]['name']),
																	)	);
		
		$this->ipsclass->html .= $plugin->acp_settings();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form_standalone("Save Plugin Settings");
		
		$this->ipsclass->admin->output();
	}
	
	function save_plugin_settings()
	{
		if(!isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['plugin']]))
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$plugin = $this->ipsclass->market_class->load_plugin($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['plugin']]);
		
		if($this->ipsclass->input['link'] != $this->ipsclass->input['plugin'] && isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
		{
			$this->ipsclass->admin->error("The plugin link id has already been used");
		}
		
		$this->check_link($this->ipsclass->input['link']);
		
		$input = array();
		$input['link'] = $this->ipsclass->input['link'];
		$input['name'] = $this->ipsclass->input['name'];
		
		$plugin->acp_save_settings($input);
		
		$this->ipsclass->DB->do_update('market_plugins',$input,"link='".$this->ipsclass->input['plugin']."'");
		
		$this->ipsclass->market_class->cache_plugins();
		
		$this->ipsclass->admin->save_log("ibMarket: Edit plugin settings (plugin: ".$this->ipsclass->input['name'].")");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=plugin","Plugin Edited");
	}
	
	function edit_permissions()
	{
		if(!isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$this->ipsclass->admin->page_title  = "Welcome to editing permissions for a plugin into ibMarket";
		$this->ipsclass->admin->page_detail = "Welcome to editing permissions for a plugin into ibMarket.";
		$this->ipsclass->admin->nav[] 		= array( $this->ipsclass->form_code.'&amp;code=plugin', 'Plugin Control'          );
		$this->ipsclass->admin->nav[] 		= array( ''                                           , 'Edit Plugin Permissions' );
		
		$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																	 	  	 1 => array( 'section'   , 'components'    ),
																			 2 => array( 'act'       , 'market'        ),
																			 3 => array( 'code'      , 'plugin'        ),
																			 4 => array( 'do'        , 'do_permission' ),
																			 5 => array( 'plugin'    , $this->ipsclass->input['link']),
																	)		);
		
		$this->build_permissions($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]['view_permission'],$this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]['use_permission']);
		
		$this->ipsclass->html .= $this->ipsclass->adskin->end_form_standalone("Save Plugin Permissions");
		
		$this->ipsclass->admin->output();
	}
	
	function save_permissions()
	{
		if(!isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['plugin']]))
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$plugin = $this->ipsclass->market_class->load_plugin($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['plugin']]);
		
		$input = array();
		
		if(is_array($this->ipsclass->input['VIEW']))
		{
			if(in_array(-1,$this->ipsclass->input['VIEW']))
			{
				$this->ipsclass->input['VIEW'] = array(-1);
			}
			$input['view_permission'] = $this->ipsclass->input['VIEW'];
		}
		else
		{
			$input['view_permission'] = array();
		}
		
		if(is_array($this->ipsclass->input['USE']))
		{
			if(in_array(-1,$this->ipsclass->input['USE']))
			{
				$this->ipsclass->input['USE'] = array(-1);
			}
			$input['use_permission'] = $this->ipsclass->input['USE'];
		}
		else
		{
			$input['use_permission'] = array();
		}
		
		$input['view_permission'] = implode(",",$input['view_permission']);
		$input['use_permission'] = implode(",",$input['use_permission']);
		
		$this->ipsclass->DB->do_update('market_plugins',$input,"link='".$this->ipsclass->input['plugin']."'");
		
		$this->ipsclass->market_class->cache_plugins();
		
		$this->ipsclass->admin->save_log("ibMarket: Permission changed for plugin ".$this->ipsclass->input['name']." (plugin: ".$plugin->name.")");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=plugin","Plugin Permissions saved");
	}
	
	function uninstall_plugin()
	{
		if(!isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$plugin = $this->ipsclass->market_class->load_plugin($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]);
		
		$plugin->acp_uninstall();
		
		$this->ipsclass->DB->build_and_exec_query(array('delete'=>'market_plugins','where'=>"link='".$this->ipsclass->input['link']."'"));
		
		$this->ipsclass->market_class->cache_plugins();
		
		$this->ipsclass->admin->save_log("ibMarket: Uninstalled plugin ".$this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]['name']." (plugin: ".$plugin->name.")");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=plugin","Plugin uninstalled");
	}
	
	function force_uninstall()
	{
		if(!file_exists(ROOT_PATH."sources/market/plugins/".$this->ipsclass->input['plugin']) || strstr($this->ipsclass->input['plugin'], 'template_plugin') !== false || $this->ipsclass->market_class->count_plugin_filename($this->ipsclass->input['plugin']) > 0)
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$plugin = $this->ipsclass->market_class->load_plugin(array('filename'=>$this->ipsclass->input['plugin']));
		
		$plugin->acp_uninstall();
		
		$this->ipsclass->admin->save_log("ibMarket: Force uninstall plugin");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=plugin","Plugin uninstalled");
	}
	
	function change_position()
	{
		if(!isset($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
		{
			$this->ipsclass->admin->error("Invalid plugin");
		}
		
		$source = $this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']];
		if($this->ipsclass->input['move'] == 'up')
		{
			if($source['position'] == 1)
			{
				$this->ipsclass->admin->error("You cannot move that plugin up another position");
			}
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['position'] == $source['position'] - 1)
				{
					$target = $plugin;
				}
			}
		}
		else
		{
			if($source['position'] == count($this->ipsclass->cache['market_plugin'][$this->ipsclass->input['link']]))
			{
				$this->ipsclass->admin->error("You cannot move that plugin down another position");
			}
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['position'] == $source['position'] + 1)
				{
					$target = $plugin;
				}
			}
		}
		
		$this->ipsclass->DB->do_update('market_plugins',array('position'=>$target['position']),"link='".$source['link']."'");
		$this->ipsclass->DB->do_update('market_plugins',array('position'=>$source['position']),"link='".$target['link']."'");
		
		$this->ipsclass->market_class->cache_plugins();
		
		$this->ipsclass->admin->save_log("ibMarket: Pluings changed position");
		
		$this->ipsclass->admin->redirect($this->ipsclass->form_code_js."&code=plugin","Plugin position changed");
	}
	
	function check_link($link)
	{
		$passed = 1;
		if(preg_match('/\W/',$link))
		{
			$passed = 0;
		}
		if(strstr($link,'&'))
		{
			$passed = 0;
		}
		if(strstr($link,'='))
		{
			$passed = 0;
		}
		if(!$passed)
		{
			$this->ipsclass->admin->error("The link id can only contain [a-zA-Z0-9] and underscore characters");
		}
	}
}
?>